//
//  RHPowerCircleView.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/9/23.
//

#import "RHPowerCircleView.h"
@interface RHPowerCircleView()
//大绿色实心圆环线宽
@property(nonatomic,assign)CGFloat bigGreenLineWidth;
//大绿色渐变圆环线宽
@property(nonatomic,assign)CGFloat bigGreenGradientLineWidth;
//绿色和灰色渐变圆环线宽
@property(nonatomic,assign)CGFloat grayLineWidth;
//圆环间的间距
@property(nonatomic,assign)CGFloat lineMargin;
//四周边距
@property(nonatomic,assign)UIEdgeInsets padding;
//灰色渐变圆环
@property(nonatomic,strong)CAGradientLayer *grayGradientLayer;
@property(nonatomic,strong)CAShapeLayer *grayShaper;
//绿色渐变圆环
@property(nonatomic,strong)CAGradientLayer *greenGradientLayer;
@property(nonatomic,strong)CAShapeLayer *greenShaper;
//大绿色渐变圆环
@property(nonatomic,strong)CAGradientLayer *bigGreenGradientLayer;
@property(nonatomic,strong)CAShapeLayer *bigGreenShaper;

//开始角度
@property(nonatomic,assign)CGFloat startAngle;
//开始角度
@property(nonatomic,assign)CGFloat endAngle;
//当前角度
@property(nonatomic,assign)CGFloat currentAngle;

@property(nonatomic,strong)UIImageView *batteryImg;

@end
@implementation RHPowerCircleView

-(instancetype)initWithFrame:(CGRect)frame
{
    if([super initWithFrame:frame]){
        [self config];
        [self createSubLayer];
    }
    return self;
}

-(void)setImgSize:(CGFloat)imgSize
{
    _imgSize = imgSize;
    self.batteryImg.size = CGSizeMake(imgSize, imgSize);
}

-(void)config{
    self.backgroundColor = [UIColor whiteColor];
    _bigGreenLineWidth = kScaleW(17);
    _bigGreenGradientLineWidth = kScaleW(22);
    _grayLineWidth = kScaleW(9);
    _lineMargin = 5;
    _padding = UIEdgeInsetsMake(5, 5, 5, 5);
    //大绿色圆环的起始角度
    _startAngle = M_PI/3*2;
    _endAngle = M_PI/3;
    _currentAngle = M_PI*1.2;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _grayGradientLayer.frame = self.bounds;
    _grayShaper.frame = self.bounds;
    _greenGradientLayer.frame = self.bounds;
    _greenShaper.frame = self.bounds;
    _bigGreenGradientLayer.frame = self.bounds;
    _bigGreenShaper.frame = self.bounds;
    self.batteryImg.center = CGPointMake(self.width/2, self.height/2);
    [self setNeedsDisplay];
}
-(void)createSubLayer
{
    //==================小灰色渐变圆环============
    _grayGradientLayer = [[CAGradientLayer alloc]init];
    _grayGradientLayer.colors = @[(id)[UIColor colorWithHexString:@"DCDCDC" alpha:0].CGColor,(id)[UIColor colorWithHexString:@"DCDCDC" alpha:0.8].CGColor];
    _grayGradientLayer.startPoint = CGPointMake(0, 1);
    _grayGradientLayer.endPoint = CGPointMake(1, 0);
    _grayGradientLayer.frame = self.bounds;
    [self.layer addSublayer:_grayGradientLayer];
    
    _grayShaper = [CAShapeLayer layer];
    _grayShaper.frame = self.bounds;
    [self.layer addSublayer:_grayShaper];
    _grayShaper.fillColor = [[UIColor clearColor]CGColor];
    _grayShaper.strokeColor = [[UIColor redColor]CGColor];
    _grayShaper.lineWidth = _grayLineWidth;
    _grayShaper.lineCap = kCALineCapRound;
    
  
    //=================小绿色渐变圆环=============
    _greenGradientLayer = [[CAGradientLayer alloc]init];
    _greenGradientLayer.colors = @[(id)[UIColor colorWithHexString:@"3ECE80" alpha:0].CGColor,(id)[UIColor colorWithHexString:@"16FF81" alpha:1].CGColor];
    _greenGradientLayer.startPoint = CGPointMake(0, 1);
    _greenGradientLayer.endPoint = CGPointMake(1, 0);
    _greenGradientLayer.frame = self.bounds;
    [self.layer addSublayer:_greenGradientLayer];
    
    _greenShaper = [CAShapeLayer layer];
    _greenShaper.frame = self.bounds;
    [self.layer addSublayer:_greenShaper];
    _greenShaper.fillColor = [[UIColor clearColor]CGColor];
    _greenShaper.strokeColor = [[UIColor redColor]CGColor];
    _greenShaper.lineWidth = _grayLineWidth;
    _greenShaper.lineCap = kCALineCapRound;
    
    //================大渐变圆环===============
    _bigGreenGradientLayer = [[CAGradientLayer alloc]init];
    _bigGreenGradientLayer.colors = @[(id)[UIColor colorWithHexString:@"46F697" alpha:0].CGColor,(id)[UIColor colorWithHexString:@"46F697" alpha:0.2].CGColor,(id)[UIColor colorWithHexString:@"46F697" alpha:1].CGColor];
    _bigGreenGradientLayer.startPoint = CGPointMake(0.5, 1);
    _bigGreenGradientLayer.endPoint = CGPointMake(0.5, 0);
    _bigGreenGradientLayer.locations = @[@(0),@(0.6),@(0.8),@(1)];
    _bigGreenGradientLayer.frame = self.bounds;
    [self.layer addSublayer:_bigGreenGradientLayer];
    
    _bigGreenShaper = [CAShapeLayer layer];
    _bigGreenShaper.frame = self.bounds;
    [self.layer addSublayer:_bigGreenShaper];
    _bigGreenShaper.fillColor = [[UIColor clearColor]CGColor];
    _bigGreenShaper.strokeColor = [[UIColor redColor]CGColor];
    _bigGreenShaper.lineWidth = _bigGreenGradientLineWidth;
    
    
    
    UIImage *img = [UIImage imageNamed:@"battery_status"];
    _batteryImg = [[UIImageView alloc]initWithImage:img];
    _batteryImg.size = CGSizeMake(60, 60);
    [self addSubview:_batteryImg];
    
    
}


-(void)drawRect:(CGRect)rect
{
    CGPoint circleCenter = CGPointMake(self.width/2, self.height/2);
    
    //小圆环的起始角度
    CGFloat grayStartAngle = _startAngle - (5*M_PI/180);;
    CGFloat grayEndAngle = _endAngle + (5*M_PI/180);;
    
   
    //减掉soc soh label的高度
    CGFloat grayRadius = MIN(self.width,self.height)/2 - _padding.top - _grayLineWidth/2;
    UIBezierPath *grayPath = [UIBezierPath bezierPathWithArcCenter:circleCenter radius:grayRadius startAngle:grayStartAngle endAngle:grayEndAngle clockwise:YES];
   
    _grayShaper.path = grayPath.CGPath;
    _grayGradientLayer.mask = _grayShaper;
    
    
    
    UIBezierPath *greenGradientPath = [UIBezierPath bezierPathWithArcCenter:circleCenter radius:grayRadius startAngle:grayStartAngle endAngle:_currentAngle clockwise:YES];
    greenGradientPath.lineWidth = _grayLineWidth;
    greenGradientPath.lineCapStyle = kCGLineCapRound;
    
    _greenShaper.path = greenGradientPath.CGPath;
    _greenGradientLayer.mask = _greenShaper;
    
    //================画绿色圆环===============
    CGFloat greenRadius = grayRadius - _grayLineWidth/2 - _lineMargin - _bigGreenLineWidth/2;
    UIBezierPath *greenPath = [UIBezierPath bezierPathWithArcCenter:circleCenter radius:greenRadius startAngle:_startAngle endAngle:_endAngle clockwise:YES];
    greenPath.lineWidth = _bigGreenLineWidth;
    greenPath.lineCapStyle = kCGLineCapRound;
    [kCOLOR_HEX(@"56C271") setStroke];
    [greenPath stroke];
    
    
   
   
    CGFloat bigGreenRadius = greenRadius - _bigGreenLineWidth/2 - _bigGreenGradientLineWidth/2;
    UIBezierPath *bigGreenGradientPath = [UIBezierPath bezierPathWithArcCenter:circleCenter radius:bigGreenRadius startAngle:_startAngle endAngle:_currentAngle clockwise:YES];
    
    _bigGreenShaper.path = bigGreenGradientPath.CGPath;
    _bigGreenGradientLayer.mask = _bigGreenShaper;

    
    //================== 刻度 =========================
    //刻度缩进的角度  5度
    CGFloat retractAngle = M_PI/180.0*5.0;
    //刻度个数
    int _scaleNumber = 36;
    CGFloat perAngle = (M_PI*2 - M_PI/3 - retractAngle*2)/_scaleNumber;
    for (int i = 0; i<=_scaleNumber; i++) {
        CGFloat lineWidth = 5;//刻度长度
        UIColor *color = [UIColor colorWithHexString:@"56C271" alpha:0.3];
        //开始角度要缩进5度
        CGFloat startAngel = _startAngle+perAngle*i +retractAngle;
        CGFloat endAngel = startAngel + M_PI/90;//画多少度,等于线宽
        if (i%4==0) {
            lineWidth = 10;
            //长指针颜色
            color = [UIColor colorWithHexString:@"56C271" alpha:0.3];
        }
        CGFloat radius = greenRadius - _bigGreenLineWidth/2-lineWidth/2;
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:circleCenter radius:radius startAngle:startAngel endAngle:endAngel clockwise:YES];
        path.lineWidth = lineWidth ;
        [color set];
        [path stroke];
    }
}
@end
